<?php
namespace Mobile\Controller;
use Think\Controller;
class ChaController extends Controller {
	//查找搜索
    public function index()
    {
        //分类遍历
        $cats = M('cat')->select();
        $cats = gettree($cats);
        $this->assign('cats',$cats);

        //选择的分类
        $id = I('get.catid');
        $this->assign('id',$id);
        //选择的
        $g = I('get.g');
        $this->assign('g',$g);
        //关键字
        $k = I('post.keyword');

        if (!empty($id)) {
            if ($g == 'new') {
                $goods = M('goods')->where(array('cat_id'=>$id,'is_new'=>1,'is_on_sale'=>1))->select();
            }elseif ($g == 'hot') {
                $goods = M('goods')->where(array('cat_id'=>$id,'is_hot'=>1,'is_on_sale'=>1))->select();
            }else {
                $goods = M('goods')->where(array('cat_id'=>$id,'is_on_sale'=>1))->select();
            }
             
        }else {
            if ($g == 'new') {
                $goods = M('goods')->where('is_new=1 and is_on_sale=1')->select();
            }elseif ($g == 'hot') {
                $goods = M('goods')->where('is_hot=1 and is_on_sale=1')->select();
            }else {
                if (!empty($k)) {
                    if (is_numeric($k)) {
                        $goods = M('goods')->where(array('goods_id'=>$k,'is_on_sale'=>1))->select();
                    }else {
                        $goods = M('goods')->where(array('goods_name'=>$k,'is_on_sale'=>1))->select();    
                    }
                }else{
                  $goods = M('goods')->where('is_on_sale=1')->select();  
                } 
            }
        }
    	$this->assign('goods',$goods);
    	
        $this->display();
    }
}